let dayjs = require('dayjs');
Component({
  data: {   //定义组件内所需要的变量
    // date：日期选择，time：时间选择
    type: "",
    // startDate：开始日期，endDate：结束日期
    selectType: 'startDate',
    startDate: '',
    endDate: '',
    dateValue: [],
    yearArr: [],
    monthArr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    dayArr: [],
    // startTime：开始时间，endTime：结束时间
    selectTimeType: 'startTime',
    startTime: '',
    endTime: '',
    dateValueTime: [],
    hourArr: [],
    minuteArr: [],
    secondArr: [],
  },
  props: {  //设置默认值 && 接收传过来的值   通过this.props.xx 获取到默认值 && 传过来的值
    type: 'date',
    nowDate: new Date(),
    startDate: new Date(),
    endDate: new Date(),
    startTime: dayjs().format('HH:mm:ss'),
    endTime: dayjs().format('HH:mm:ss'),
    interval: 10,
    onAffirm: (data) => console.log(data),
  },
  didMount: function didMount() { //页面渲染完毕后执行 (组件的生命周期函数)
    this.setData({
      type: this.props.type
    })

    if (this.props.type == 'date') {
      this.init()
    }
    if (this.props.type == 'time') {
      this.initTime()
    }
  },
  didUpdate: function didUpdate(preProps) {
    // dayjs().format('YYYY-MM-DD')
  },
  methods: {
    // 日期选择器
    init() {
      this.props.startDate = this.props.startDate || new Date()
      this.props.endDate = this.props.endDate || new Date()
      let nowDateTime = dayjs(this.props.nowDate).format('YYYY-MM-DDTHH:mm:ss');
      let startDateArr = dayjs(this.props.startDate).format('YYYY-MM-DD').split("-");
      let startTimeArr = dayjs(this.props.startDate).format('HH:mm:ss').split(":");
      let endDateArr = dayjs(this.props.endDate).format('YYYY-MM-DD').split("-");
      let endTimeArr = dayjs(this.props.endDate).format('HH:mm:ss').split(":");
      let interval = this.props.interval;
      let days = dayjs(this.props.nowDate).daysInMonth()

      // 计算年
      let starYear = Number(startDateArr[0]) - 10;
      let endYear = Number(startDateArr[0]) + 10;
      for (let i = starYear; i <= endYear; i++) {
        this.data.yearArr.push(i)
      }
      this.setData({
        yearArr: this.data.yearArr
      })

      // 计算日
      for (let i = 1; i <= days; i++) {
        this.data.dayArr.push(i)
      }
      this.setData({
        dayArr: this.data.dayArr
      })

      // 计算开始位置
      this.data.dateValue[0] = this.data.yearArr.findIndex(n => {
        return n == startDateArr[0]
      })
      this.data.dateValue[1] = this.data.monthArr.findIndex(n => {
        return n == startDateArr[1]
      })
      this.data.dateValue[2] = this.data.dayArr.findIndex(n => {
        return n == startDateArr[2]
      })

      this.setData({
        dateValue: this.data.dateValue,
        startDate: startDateArr.join('-'),
        endDate: endDateArr.join('-'),
      })

    },

    // 开始
    selectStartDate(e) {
      let date = this.data.startDate.split('-')
      this.data.dateValue[0] = this.data.yearArr.findIndex(n => {
        return n == date[0]
      })
      this.data.dateValue[1] = this.data.monthArr.findIndex(n => {
        return n == date[1]
      })
      this.data.dateValue[2] = this.data.dayArr.findIndex(n => {
        return n == date[2]
      })
      let type = e.target.dataset.type
      this.setData({
        selectType: type,
        dateValue: this.data.dateValue
      })
    },
    // 结束
    selectEndDate(e) {
      let date = this.data.endDate.split('-')
      this.data.dateValue[0] = this.data.yearArr.findIndex(n => {
        return n == date[0]
      })
      this.data.dateValue[1] = this.data.monthArr.findIndex(n => {
        return n == date[1]
      })
      this.data.dateValue[2] = this.data.dayArr.findIndex(n => {
        return n == date[2]
      })
      let type = e.target.dataset.type
      this.setData({
        selectType: type,
        dateValue: this.data.dateValue
      })
    },

    onChangePicker(e) {
      let value = e.detail.value
      let y = this.data.yearArr[value[0]]
      let m = this.data.monthArr[value[1]]
      let d = this.data.dayArr[value[2]]
      if (this.data.selectType == 'startDate') {
        let arr = this.data.startDate.split('-')
        if (arr[0] != y || arr[1] != m) {
          let days = dayjs(y + '-' + m + '-' + d).daysInMonth()
          let arrd = []
          for (let i = 1; i <= days; i++) {
            arrd.push(i)
          }
          value[2] = 0
          d = 1
          this.setData({
            dayArr: arrd,
            dateValue: value
          })
        }
        this.setData({
          startDate: dayjs(y + '-' + m + '-' + d).format('YYYY-MM-DD')
        })
      }
      if (this.data.selectType == 'endDate') {
        let arr = this.data.endDate.split('-')
        if (arr[0] != y || arr[1] != m) {
          let days = dayjs(y + '-' + m + '-' + d).daysInMonth()
          let arrd = []
          for (let i = 1; i <= days; i++) {
            arrd.push(i)
          }
          value[2] = 0
          d = 1
          this.setData({
            dayArr: arrd,
            dateValue: value
          })
        }
        this.setData({
          endDate: dayjs(y + '-' + m + '-' + d).format('YYYY-MM-DD')
        })
      }
    },

    // -------------
    // 时间选择器
    initTime() {
      let startTimeArr = this.props.startTime.split(':')
      let endTimeArr = this.props.endTime.split(':')
      console.log(startTimeArr)
      console.log(endTimeArr)
      for (let i = 0; i < 24; i++) {
        let num = this.addPreZero(i)
        this.data.hourArr.push(num)
      }
      for (let i = 0; i < 60; i++) {
         let num = this.addPreZero(i)
        this.data.minuteArr.push(num)
      }
      this.setData({
        hourArr: this.data.hourArr,
        minuteArr: this.data.minuteArr,
        startTime: startTimeArr[0] + ":" + startTimeArr[1],
        endTime: endTimeArr[0] + ":" + endTimeArr[1],
      })
      this.selectStartTime()
    },

    selectStartTime() {
      let date = this.data.startTime.split(':')
      this.data.dateValueTime[0] = this.data.hourArr.findIndex(n => {
        return n == date[0]
      })
      this.data.dateValueTime[1] = this.data.minuteArr.findIndex(n => {
        return n == date[1]
      })
      this.setData({
        selectTimeType: 'startTime',
        dateValueTime: this.data.dateValueTime
      })
    },

    selectEndTime() {
      let date = this.data.endTime.split(':')
      this.data.dateValueTime[0] = this.data.hourArr.findIndex(n => {
        return n == date[0]
      })
      this.data.dateValueTime[1] = this.data.minuteArr.findIndex(n => {
        return n == date[1]
      })
      this.setData({
        selectTimeType: 'endTime',
        dateValueTime: this.data.dateValueTime
      })
    },

    onChangePickerTime(e) {
      let value = e.detail.value
      let h = this.data.hourArr[value[0]]
      let m = this.data.minuteArr[value[1]]
      if (this.data.selectTimeType == 'startTime') {
        this.setData({
          startTime: h + ":" + m
        })
      }
      if (this.data.selectTimeType == 'endTime') {
        this.setData({
          endTime: h + ":" + m
        })
      }
    },

    // 日期完成或取消
    onaffirmcancel(e) {
      let type = e.target.dataset.type
      let data = {
        result: true,
        startDate: dayjs(this.data.startDate).format('YYYY-MM-DD'),
        endDate: dayjs(this.data.endDate).format('YYYY-MM-DD')
      }
      if (type == 0) {
        data.result = false
      }
      this.props.onAffirm(data);
    },

    // 时间完成或取消
    onaffirmcancelTime(e) {
      let type = e.target.dataset.type
      let arr1 = this.data.startTime.split(":")
      let arr2 = this.data.endTime.split(":")
      for (let i in arr1) {
        arr1[i] = this.addPreZero(arr1[i])
      }
      for (let i in arr2) {
        arr2[i] = this.addPreZero(arr2[i])
      }
      let data = {
        result: true,
        startTime: arr1[0] + ":" + arr1[1],
        endTime: arr2[0] + ":" + arr2[1]
      }
      if (type == 0) {
        data.result = false
      }
      this.props.onAffirm(data);
    },

    // 前置补0
    addPreZero(num) {
      let val = parseInt(num)
      if (val < 10) {
        return '0' + val;
      } else {
        return val
      }
    }
  }
});